﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ContactManager.Common;
using ContactManager.Entity;
using System.Data.OleDb;

namespace ContactManager
{
    public partial class TypeAndSubFrm : AlSkin.AlForm.AlBaseForm
    {

        public TypeAndSubFrm()
        {
            InitializeComponent();
        }

        private void TypeAndSubFrm_Load(object sender, EventArgs e)
        {
            this.listBoxType.Items.Clear();
            this.txtTypeName.Clear();
            this.combInPay.Items.Clear();
            //初始化类型
            string sql = "select * from typeinc";
            DataSet ds = AccessHelper.ExecuteQuery(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                TypeInc ti = new TypeInc();
                ti.Id = dr[0].ToString();
                ti.Name = dr[1].ToString();
                ti.Inpay = dr[2].ToString();
                this.listBoxType.Items.Add(ti);
            }
            //初始化所属combox
            ComboBoxItem ci1 = new ComboBoxItem();
            ci1.Value = 0;
            ci1.Text = "收入\\贷入";
            ComboBoxItem ci2 = new ComboBoxItem();
            ci2.Value = 1;
            ci2.Text = "支出\\借出";
            this.combInPay.Items.Add(ci1);
            this.combInPay.Items.Add(ci2);

        }

        private void listBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxType.SelectedItem != null)
            {
                TypeInc ti = (TypeInc)this.listBoxType.SelectedItem;

                //设置表单
                this.txtTypeName.Text = ti.Name;
                this.combInPay.SelectedIndex = Convert.ToInt32(ti.Inpay);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string typeName = this.txtTypeName.Text.Trim();
            string inPay = "";
            ComboBoxItem ci = (ComboBoxItem)this.combInPay.SelectedItem;
            if (ci != null)
            {
                inPay = ci.Value.ToString();
            }

            if (typeName == "")
            {
                MessageBox.Show("请输入类型名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (inPay == "")
            {
                MessageBox.Show("请选择所属", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string addSql = "insert into typeinc(t_name,t_inpay) values(?,?)";
            OleDbParameter[] addParameters = { 
                                                new OleDbParameter("@t_name",typeName),
                                                new OleDbParameter("@t_inpay",inPay)
                                              };
            if (AccessHelper.ExecuteNonQuery(addSql, addParameters) > 0)
            {
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.TypeAndSubFrm_Load(this, null);
            }

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string typeName = this.txtTypeName.Text.Trim();
            string inPay = "";
            string id = "";
            ComboBoxItem ci = (ComboBoxItem)this.combInPay.SelectedItem;
            if (ci != null)
            {
                inPay = ci.Value.ToString();
            }

            if (typeName == "")
            {
                MessageBox.Show("请单击列表，选择一个分类", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            TypeInc ti = (TypeInc)this.listBoxType.SelectedItem;
            if (ci != null)
            {
                id = ti.Id;
            }
            if (id == "")
            {
                MessageBox.Show("请单击列表，选择一个分类", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string updateSql = "update typeinc set t_name = ?,t_inpay = ? where t_id = ?";
            OleDbParameter[] updateParameters = { 
                                                    new OleDbParameter("@t_name",typeName),
                                                    new OleDbParameter("@t_inpay",inPay),
                                                    new OleDbParameter("@t_id",id)
                                                  };
            if (AccessHelper.ExecuteNonQuery(updateSql, updateParameters) > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.TypeAndSubFrm_Load(this, null);
            }
        }

        private void DeleteTypeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TypeInc ti = (TypeInc)this.listBoxType.SelectedItem;
            if (ti == null)
            {
                MessageBox.Show("请选择一个类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string sql = "delete * from typeinc where t_id = ?";
            OleDbParameter[] parameters = { new OleDbParameter("@t_id",ti.Id) };

            if (DialogResult.Yes == MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
            {
                if (AccessHelper.ExecuteNonQuery(sql, parameters) > 0)
                {
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            this.TypeAndSubFrm_Load(this, null);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtTypeName.Clear();
            this.combInPay.SelectedIndex = -1;
        }

        private void TypeAndSubFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("您确定要关闭吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
            {
                HomeFrm.tsfrm = null;
            }
            else
            {
                e.Cancel = true;
            }
        }


    }
}
